package com.example.dell.fastshopping.user.bank;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.example.dell.fastshopping.network.ApiService;
import com.example.dell.fastshopping.network.NetCallback;
import com.example.dell.fastshopping.network.RetrofitClient;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.user.entity.Balance;
import com.example.dell.fastshopping.utils.KLog;
import com.example.dell.fastshopping.utils.SPDao;
import com.google.gson.GsonBuilder;

import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by ${YSH} on 2019/5/21.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class BankModel extends ViewModel {
    private MutableLiveData<List<BankCard>> bankcard = new MutableLiveData<>();
    private MutableLiveData<BaseResponse<String>> checkBankcard = new MutableLiveData<>();
    private MutableLiveData<BaseResponse> addBank = new MutableLiveData<>();
    private MutableLiveData<BaseResponse> deleteBank = new MutableLiveData<>();
    private MutableLiveData<BaseResponse> editBank = new MutableLiveData<>();
    private ApiService service = RetrofitClient.getInstance().create(ApiService.class);


    public MutableLiveData<List<BankCard>> getMyBanlCard() {

        RetrofitClient.execute(service.getBankCard(), new NetCallback<BaseResponse<List<BankCard>>>() {
            @Override
            public void netCallback(BaseResponse<List<BankCard>> o) {
                bankcard.setValue(o.getData());
            }
        });
        return bankcard;
    }

    public MutableLiveData<BaseResponse> addBankCard(String idCard, String phone_, String bank, String userName, String cardId) {
        int userId = SPDao.getUserId();
        HashMap<String, Object> map = new HashMap<>();
        map.put("bank", bank);
        map.put("bankName", bank);
        map.put("cardId", cardId);
        map.put("idCard", idCard);
        map.put("isDefault", 0);
        map.put("phone", phone_);
        map.put("userId", userId);
        map.put("userName", userName);
        map.put("status", 0);
        String toJson = new GsonBuilder().create().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), toJson);
        RetrofitClient.execute(service.addBankCard(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse o) {
                if (o.isSuccess()) {
                    addBank.setValue(o);
                }
            }
        });
        return addBank;
    }

    public MutableLiveData<BaseResponse> deleteBankCard(int id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);

        String s = new GsonBuilder().create().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), s);
        RetrofitClient.execute(service.deleteBank(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse o) {
                if (o.isSuccess()) {
                    deleteBank.setValue(o);
                }
            }
        });
        return deleteBank;
    }

    public LiveData<BaseResponse> editBankCard(int id, String idCard, String phone, String bank, String userName, String cardId) {
        int userId = SPDao.getUserId();
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("bank", bank);
        map.put("bankName", bank);
        map.put("cardId", cardId);
        map.put("idCard", idCard);
        map.put("isDefault", 0);
        map.put("phone", phone);
        map.put("userId", userId);
        map.put("userName", userName);
        map.put("status", 0);

        String toJson = new GsonBuilder().create().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), toJson);
        RetrofitClient.execute(service.editBankCard(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse o) {
                if (o.isSuccess()) {
                    editBank.setValue(o);
                }
            }
        });
        return editBank;
    }

    public MutableLiveData<BaseResponse<String>> checkBankCard(String cardN) {
        RetrofitClient.execute(service.checkBankCard(cardN), new NetCallback<BaseResponse<String>>() {
            @Override
            public void netCallback(BaseResponse<String> o) {
                checkBankcard.setValue(o);
            }
        });
        return checkBankcard;
    }
}
